<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传图片</title>
    <link rel="stylesheet" href="resources/layui/css/layui.css">
</head>
<body style="margin: 20px">


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>数据表格</legend>
</fieldset>
<div>
    <script type="text/html" id="attachmentTableBar">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script type="text/html" id="attachmentTableToolBar">
        <a class="layui-btn" lay-event="add">上传文件</a>
    </script>
    <table id="attachmentTable" class="layui-hide" lay-filter="attachmentTable"></table>
</div>

<!--上传文件弹出层内容开始-->
<div id="uploadFileDiv" style="display: none" class="layui-container">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>上传多张图片</legend>
    </fieldset>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="chooseFile">多图片上传</button>
        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
            预览图：
            <div class="layui-upload-list" id="prvView"></div>
        </blockquote>
    </div>
</div>
<!--上传文件弹出层内容结束-->
</body>
<script type="text/javascript" src="resources/layui/layui.js">
</script>
<script>
    layui.use(['upload','jquery','layer','table'],function () {
        let upload=layui.upload;
        let $=layui.jquery;
        let layer=layui.layer;
        let table=layui.table;
        //多图片上传
        upload.render({
            elem: '#chooseFile'
            ,url: '/attachment/doUpload' //此处配置你自己的上传接口即可
            ,multiple: true
            ,field:'mf'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#prvView').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            ,done: function(res){
                //上传完毕
                console.log(res);
                //关闭弹出层
                layer.close(mainIndex)
                attachmentTable.reload();
            }
        });


        //渲染数据表格、
        let attachmentTable=table.render({
            elem: '#attachmentTable'
            ,url:'/attachment/loadAllAttachmentForPage'
            ,toolbar: '#attachmentTableToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print']
            ,title: '附件数据表'
            ,cols: [[
                {field:'id', title:'ID', width:80, fixed: 'left'}
                ,{field:'name', title:'名称', }
                ,{field:'path', title:'图片',  templet: function(d){
                        return '<img src="'+d.path+'" width="30" height="30">'
                    }}
                ,{field:'type', title:'类型', }
                ,{field:'size', title:'大小', }
                ,{field:'createTime', title:'上传时间'}
                ,{fixed: 'right', title:'操作', toolbar: '#attachmentTableBar'}
            ]]
            ,page: true
        })
        //监听头工具条的事件
        table.on("toolbar(attachmentTable)",function (obj) {
            if(obj.event='add'){
                openAddLayer();
            }
        })

        //鉴听行工具条的事件
        table.on("tool(attachmentTable)",function (obj) {
            if(obj.event=='del'){
                layer.msg("del")
                layer.confirm("你确定要删除【"+obj.data.name+"】这张图片吗?",function () {
                    $.post("/attachment/delAttachment",{id:obj.data.id},function (res) {
                        if(res.code==200){
                            attachmentTable.reload();
                        }
                        layer.msg(res.msg);
                    })
                })
            }
        })

        //打开文件上传
        let mainIndex=0;
        function openAddLayer() {
            mainIndex=layer.open({
                type: 1,
                title:'上传文件',
                content:$("#uploadFileDiv"),
                area:['600px','800px'],
                success:function () {
                    $('#prvView').html("")
                }
            })
        }
    })
</script>

</html>